using System;
using System.Collections.Generic;
using System.Text;
using Fieldbook.Entities;
using Fieldbook.DAL;
using Fieldbook.Common;

namespace Fieldbook.Business
{
    public static class OperationController
    {
        #region Operation category
        public static List<OperationCategory> LoadOperationCategories(long userID)
        {
            List<OperationCategory> res = null;
            try
            {
                res = OperationCategoryDAO.LoadOperationCategories(userID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading operation categories", ex);
            }
            return res;
        }
        
        public static bool InsertOperationCategory(OperationCategory category)
        {
            bool res = false;
            try
            {
                res = OperationCategoryDAO.InsertOperationCategory(category);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error inserting operation category", ex);
            }
            return res;
        }

        public static bool InsertOperationCategory(string description)
        {
            OperationCategory category = new OperationCategory();
            category.Description = description;
            return InsertOperationCategory(category);
        }

        public static bool InsertOperationCategory(string description, long userID)
        {
            bool res = false;
            try
            {
                long? houseHoldID = HouseholdDAO.GetHouseHoldID(userID);
                if (houseHoldID != null)
                {
                    OperationCategory category = new OperationCategory();
                    category.Description = description;
                    category.HouseHoldID = houseHoldID;
                    res = InsertOperationCategory(category);
                }
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error inserting operation category", ex);
            }
            return res;
        }
        #endregion

        #region Operations
        public static List<Operation> LoadOperations(long userID)
        {
            List<Operation> res = null;
            try
            {
                res = OperationDAO.LoadOperations(userID, Operation.OperationType.RequestedService);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading operations", ex);
            }
            return res;
        }

        public static List<Operation> LoadOperations(long userID, long categoryID)
        {
            List<Operation> res = null;
            try
            {
                res = OperationDAO.LoadOperations(userID, categoryID, Operation.OperationType.RequestedService);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading operations", ex);
            }
            return res;
        }

        public static bool InsertOperation(Operation operation)
        {
            bool res = false;
            try
            {
                res = OperationDAO.InsertOperation(operation);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error inserting operation", ex);
            }
            return res;
        }

        public static bool InsertOperation(long categoryID, string description, long? houseHoldID, Operation.OperationType operationType)
        {
            Operation operation = new Operation();
            operation.CategoryID = categoryID;
            operation.Description = description;
            operation.HouseHoldID = houseHoldID;
            operation.OperationTypeID = operationType;
            return InsertOperation(operation);
        }
        #endregion
    }
}
